package com.company.project.temp;

import com.mongodb.BasicDBObject;
import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Hello world!
 *
 */
public class PointLog {

	private static Map<String, Integer> map = new HashMap();

	public static void main(String[] args) {
		test2();


	}

	private static Long sum = 0l;




	private static BasicDBObject getLikeStr(String findStr) {
		Pattern pattern = Pattern.compile("^.*" + findStr + ".*$", Pattern.CASE_INSENSITIVE);
		return new BasicDBObject("$regex", pattern);
	}


	private static void test2() {


		MongoClient mongo = new MongoClient("pre_test.72solo.com", 27017);
		MongoDatabase db = mongo.getDatabase("inno72_prod_12031530");
		MongoCollection<Document> collection = db.getCollection("PointLog");
		BasicDBObject query = new BasicDBObject();
		query.put("tag", getLikeStr("\"sdFree\":0"));

		Set<String> machineIds = new HashSet<>();

		collection.find(query).forEach(new Block<Document>() {

			@Override
			public void apply(Document next) {
				String tag = (String) next.get("tag");

				if (StringUtils.isNotEmpty(tag)) {
					try {
						String accid = FastJsonUtils.getString(tag, "accid");
						String sdFree = FastJsonUtils.getString(tag, "sdFree");
						String machineId = FastJsonUtils.getString(tag, "machineId");
						if (StringUtils.isNotEmpty(sdFree) && sdFree.equals("0")) {
							System.out.println(sdFree + " " + machineId);
							machineIds.add(machineId);
						}
					} catch (Exception e) {
						// e.printStackTrace();
					}
				}
			}

		});

		System.out.println("++++++++++++++++");
		for (String machineId : machineIds) {
			System.out.println(machineId);
		}

		mongo.close();


	}


}
